var chaxun = new Vue({
	el: '#searchPage',
	data: {
		website: "换源",
		name: '',
		novels: [],
		clickable: false,
		icon_what: 'glyphicon-search',
		localData: {  //使用pass表示该数据为空，所有书对象里都是数组类型，存放最近的三个对象
			latelyType: [{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			}, {
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			},{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			},{
				id: '',
				title: '',
				icon: '',
				introduce: '',
				go: ""
			}], //最近浏览的小说类别
			latelyList: [{ // 最近看过的小说
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			},{
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			},{
				title: '', // 书名
				href: '',  // 链接
				latelyCatalogue: [{ // 最近看的章节
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				},{
					chapter: '', // 章节名
					href: ''  // 链接
				}]
			}],
			latelySearch: ['', '', '', '', '', ''] //最近查询（允许存放5个结果）
		}
	},
	methods: {
		setWebsite: function (data) {
			switch(data){
				case '笔趣阁':
					if(!$("li.bqg.disabled").length) this.website = data;
					break;
				case '顶点小说':
					if(!$("li.ddxs.disabled").length) this.website = data;
					break;
				case '三江阁':
					if(!$("li.sjg.disabled").length) this.website = data;
					break;
				case '小小书屋':
					if(!$("li.xxsw.disabled").length) this.website = data;
					break;
			}
        },
		gotoNovelPage: function () {
			if(!this.name || this.clickable) return;
			this.clickable = true;
			this.icon_what = 'glyphicon-hourglass';
			this.novels = '';
			var that = this;
			$('img#load').removeClass('hidden');
			$.ajax({
				type : 'POST',
				url : '/novel/novel_data/',
				dataType : "json",//数据类型为json
				data: {
					what: 'search',
					site: that.wsite,
					name: that.name
				},
				success: function (data) {
					if(data.result){
						if(data.data.length){
							that.novels = data.data;
						}else{
							that.novels = [{
								title: '抱歉',
								author: '当前关键词检索无结果，请检查是否关键词输入错误',
								updateChapter: ''
							}]
						}
					}
					that.clickable = false;
					that.icon_what = 'glyphicon-search';
					$('img#load').addClass('hidden');
                },
				error:function(msg){
					console.log("ajax error msg",msg);
					$('img#load').addClass('hidden');
					that.clickable = false;
					that.icon_what = 'glyphicon-search';
					$('#promptbtn').click();
				}
			});
			var dt = this.localData.latelySearch;
			var i = 0;
			for(; i < dt.length; i++){
				if(dt[i] === this.name){
					break;
				}else if(!dt[i]){
					dt[i] = this.name;
					break;
				}
			}
			if(i === dt.length){
				dt.shift();
				dt.push(this.name);
			}
			try{
				localStorage.setItem('lately', JSON.stringify(this.localData));
			}catch (err){
				console.log(err);
			}
        },
		gotoSearch: function (keyword) {
			this.name = keyword;
			this.gotoNovelPage();
        }
	},
	computed: {
		wsite: function () {
			switch(this.website){
				case '笔趣阁':
					return 'bqg';
				case '顶点小说':
					return 'ddxs';
				case '三江阁':
					return 'sjg';
				case '小小书屋':
					return 'xxsw';
				default:
					return 'ddxs';
			}
        }
	},
	created: function () {
		try {
			var data = localStorage.getItem('lately');
			if(data){
				this.localData = JSON.parse(data);
			}else{
				localStorage.setItem('lately', JSON.stringify(this.localData));
			}
		} catch (err) {
			console.log(err);
		}
    },
	mounted: function(){
		try{
			this.website = $('span.website')[0].innerHTML;
			this.name = $('span.nvname')[0].innerHTML;
			this.gotoNovelPage();
		}catch (err) {
			console.log(err);
			this.website = "换源";
			this.name = '';
		}
	}
});